package com.hzeport.bgs22emduty.util;

import com.hzeport.bgs22emduty.entity.ParamHolidays;
import com.hzeport.bgs22emduty.entity.ParamScheduleSetting;
import org.springframework.util.CollectionUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class DateUtil {

    public static Long getDaysOfMonth(Integer year,Integer month)throws Exception{
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
               return 31l;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30l;
            case 2:
                // 根据年来判断2月份的天数
                if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {
                    return 29l;
                } else {
                    return 28l;
                }
            default:
                throw  new Exception("输入的月份不合法");
        }

    }

    public static int getDaysOfQuarter(Integer year, Integer quarter) throws Exception {
        switch (quarter){
            case 1:
                if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {
                    return 91;
                } else {
                    return 90;
                }
            case 2:
                return 91;
            case 3:
            case 4:
                return 92;
            default:
                throw  new Exception("输入的季度不合法");
        }
    }

    public static String getDaysOfWeek(Long sYear, Long sMonth, int day) {
        LocalDate localDate= LocalDate.of((int)(long)sYear,(int)(long)sMonth,day);
        DayOfWeek dayOfWeek=localDate.getDayOfWeek();
        switch (dayOfWeek){
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                return "";
        }
    }

    public static Integer getCommonCount(Long startYear, Long startMonth, Long endYear, Long endMonth, List<ParamHolidays> holidays, List<ParamHolidays> importantDays, List<ParamHolidays> hoildayExchanges,List<ParamScheduleSetting> rcSetting,List<ParamScheduleSetting> zmSetting) {
        LocalDate start=LocalDate.of((int)(long)startYear,(int)(long)startMonth,1);
        LocalDate end=LocalDate.of((int)(long)endYear,(int)(long)endMonth,1);
        LocalDate index=start;
        List<LocalDate> hoildayDate=holidays.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());//节假日天数
        List<LocalDate> importantDate=importantDays.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());//重要时期天数
        List<LocalDate> hoildayExchangesDate=hoildayExchanges.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());//节假日调班
        Integer count=0;
        while (index.compareTo(end)<0){
            if (hoildayDate.contains(index)||importantDate.contains(index)){
                index=index.plusDays(1);
                continue;
            }
            DayOfWeek day=DayOfWeek.of(index.get(ChronoField.DAY_OF_WEEK));
            switch (day){
                case SATURDAY:
                case SUNDAY:
                    if (hoildayExchangesDate.contains(index)){
                        count+=rcSetting.size();
                    }
//                    else {
//                        count+=zmSetting.size();
//                    }
//                    count+=zmSetting.size();
                    break;
                default:
                    count+=rcSetting.size();
            }
            index=index.plusDays(1);
        }
//        if (count>0){
//        }
        return count;
    }

    public static Integer getWeekendCount(Long startYear, Long startMonth, Long endYear, Long endMonth, List<ParamHolidays> holidays, List<ParamHolidays> importantDays, List<ParamHolidays> hoildayExchanges,List<ParamScheduleSetting> rcSetting,List<ParamScheduleSetting> zmSetting) {
        LocalDate start=LocalDate.of((int)(long)startYear,(int)(long)startMonth,1);
        LocalDate end=LocalDate.of((int)(long)endYear,(int)(long)endMonth,1);
        LocalDate index=start;
        List<LocalDate> hoildayDate=holidays.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());
        List<LocalDate> importantDate=importantDays.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());
        List<LocalDate> hoildayExchangesDate=hoildayExchanges.stream().map(ParamHolidays::getHolidayDate).collect(Collectors.toList());
        Integer count=0;
        while (index.compareTo(end)<0){
            if (hoildayDate.contains(index)||importantDate.contains(index)){
                index=index.plusDays(1);
                continue;
            }
            DayOfWeek day=DayOfWeek.of(index.get(ChronoField.DAY_OF_WEEK));
            switch (day){
                case SATURDAY:
                case SUNDAY:
                    if (hoildayExchangesDate.contains(index)){
//                        count+=rcSetting.size();
                    }else {
                        count+=zmSetting.size();
                    }
//                    count+=zmSetting.size();
                    break;
                default:
//                    count+=rcSetting.size();
            }
            index=index.plusDays(1);
        }
//        if (count>0){
//        }
        return count;
    }

    public static List<LocalDate> getDatesForAQuater(String year,String quarter)throws Exception {
        switch (quarter){
            case "1":
                LocalDate start=LocalDate.of(Integer.valueOf(year),1,1);
                LocalDate end=LocalDate.of(Integer.valueOf(year),3,31);
                return Arrays.asList(start,end);
            case "2":
                 start=LocalDate.of(Integer.valueOf(year),4,1);
                 end=LocalDate.of(Integer.valueOf(year),6,30);
                return Arrays.asList(start,end);
            case "3":
                start=LocalDate.of(Integer.valueOf(year),7,1);
                end=LocalDate.of(Integer.valueOf(year),9,30);
                return Arrays.asList(start,end);
            case "4":
                 start=LocalDate.of(Integer.valueOf(year),10,1);
                 end=LocalDate.of(Integer.valueOf(year),12,31);
                return Arrays.asList(start,end);
            default:
                throw new Exception("不合法的季度"+quarter);
        }
    }
    public static List<LocalDate> getDatesForThisMONTH(Integer year,Integer month) {
                LocalDate start=LocalDate.now().withYear(year).withMonth(month).withDayOfMonth(1);
                LocalDate end=LocalDate.now().withYear(year).withMonth(month).plusMonths(1).withDayOfMonth(1).minusDays(1);
                return Arrays.asList(start,end);
    }

    public static List<LocalDate> getDatesForToday() {
        LocalDate start=LocalDate.now();
        LocalDate end=LocalDate.now().plusDays(2);
        return Arrays.asList(start,end);
    }

    public static  boolean isLastDayOfMonth(LocalDate localDateTime){
        int month=localDateTime.getMonth().getValue();
        if (localDateTime.plusDays(1l).getMonth().getValue()==month){
            return false;
        }else{
            return true;
        }
    }
//    public static void main(String[] args) {
//        System.out.println(getDaysOfWeek(2022l,12l,8));
//    }
}
